﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;
using Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// Returned by a MultigetSliceQuery (multiget_slice thrift call)
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class RowsImpl<K, N, V> : Rows<K, N, V>
    {
        protected int AsCount = 0;

        public RowsImpl(Dictionary<K, List<Column>> thriftRet, Serializer<N> nameSerializer,
            Serializer<V> valueSerializer)
        {
            if (thriftRet == null || nameSerializer == null || valueSerializer == null)
            {
                throw new NullReferenceException("Thrift columns, Name serializer and Value serializer cannot be null");
            }
            Rows = new Dictionary<K, Row<K, N, V>>(thriftRet.Count);
            foreach (K key in thriftRet.Keys)
            {
                Rows[key] = new RowImpl<K, N, V>(key, thriftRet[key], nameSerializer, valueSerializer);
            }
        }
        protected RowsImpl()
        {
            Rows = new Dictionary<K, Row<K,N,V>>();
        }
        protected Dictionary<K, Row<K, N, V>> Rows
        {
            get;
            set;
        }
        public Row<K, N, V> GetByKey(K key)
        {
            return Rows[key];
        }
        public int Count
        {
            get
            {
                return Rows.Count;
            }
        }
        public IEnumerator<Row<K, N, V>> GetEnumerator()
        {
            return Rows.Values.GetEnumerator();
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public override string ToString()
        {
            return "Rows(" + Rows + ")";
        }
    }
}
